package org.jeecg.modules.checkInventory.service.impl;

import org.jeecg.modules.checkInventory.entity.CheckInventory;
import org.jeecg.modules.checkInventory.entity.CheckInventoryShelf;
import org.jeecg.modules.checkInventory.entity.CheckInventorySku;
import org.jeecg.modules.checkInventory.entity.CheckInventoryResult;
import org.jeecg.modules.checkInventory.entity.CheckInventoryDifference;
import org.jeecg.modules.checkInventory.mapper.CheckInventoryShelfMapper;
import org.jeecg.modules.checkInventory.mapper.CheckInventorySkuMapper;
import org.jeecg.modules.checkInventory.mapper.CheckInventoryResultMapper;
import org.jeecg.modules.checkInventory.mapper.CheckInventoryDifferenceMapper;
import org.jeecg.modules.checkInventory.mapper.CheckInventoryMapper;
import org.jeecg.modules.checkInventory.service.ICheckInventoryService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 仓库盘点
 * @Author: jeecg-boot
 * @Date:   2023-12-18
 * @Version: V1.0
 */
@Service
public class CheckInventoryServiceImpl extends ServiceImpl<CheckInventoryMapper, CheckInventory> implements ICheckInventoryService {

	@Autowired
	private CheckInventoryMapper checkInventoryMapper;
	@Autowired
	private CheckInventoryShelfMapper checkInventoryShelfMapper;
	@Autowired
	private CheckInventorySkuMapper checkInventorySkuMapper;
	@Autowired
	private CheckInventoryResultMapper checkInventoryResultMapper;
	@Autowired
	private CheckInventoryDifferenceMapper checkInventoryDifferenceMapper;
	
	@Override
	@Transactional
	public void saveMain(CheckInventory checkInventory, List<CheckInventoryShelf> checkInventoryShelfList,List<CheckInventorySku> checkInventorySkuList,List<CheckInventoryResult> checkInventoryResultList,List<CheckInventoryDifference> checkInventoryDifferenceList) {
		checkInventoryMapper.insert(checkInventory);
		if(checkInventoryShelfList!=null && checkInventoryShelfList.size()>0) {
			for(CheckInventoryShelf entity:checkInventoryShelfList) {
				//外键设置
				entity.setInventoryOrder(checkInventory.getInventoryOrder());
				checkInventoryShelfMapper.insert(entity);
			}
		}
		if(checkInventorySkuList!=null && checkInventorySkuList.size()>0) {
			for(CheckInventorySku entity:checkInventorySkuList) {
				//外键设置
				entity.setInventoryOrder(checkInventory.getInventoryOrder());
				checkInventorySkuMapper.insert(entity);
			}
		}
		if(checkInventoryResultList!=null && checkInventoryResultList.size()>0) {
			for(CheckInventoryResult entity:checkInventoryResultList) {
				//外键设置
				entity.setInventoryOrder(checkInventory.getInventoryOrder());
				checkInventoryResultMapper.insert(entity);
			}
		}
		if(checkInventoryDifferenceList!=null && checkInventoryDifferenceList.size()>0) {
			for(CheckInventoryDifference entity:checkInventoryDifferenceList) {
				//外键设置
				entity.setInventoryOrder(checkInventory.getInventoryOrder());
				checkInventoryDifferenceMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(CheckInventory checkInventory,List<CheckInventoryShelf> checkInventoryShelfList,List<CheckInventorySku> checkInventorySkuList,List<CheckInventoryResult> checkInventoryResultList,List<CheckInventoryDifference> checkInventoryDifferenceList) {
		checkInventoryMapper.updateById(checkInventory);
		
		//1.先删除子表数据
		checkInventoryShelfMapper.deleteByMainId(checkInventory.getId());
		checkInventorySkuMapper.deleteByMainId(checkInventory.getId());
		checkInventoryResultMapper.deleteByMainId(checkInventory.getId());
		checkInventoryDifferenceMapper.deleteByMainId(checkInventory.getId());
		
		//2.子表数据重新插入
		if(checkInventoryShelfList!=null && checkInventoryShelfList.size()>0) {
			for(CheckInventoryShelf entity:checkInventoryShelfList) {
				//外键设置
				entity.setInventoryOrder(checkInventory.getInventoryOrder());
				checkInventoryShelfMapper.insert(entity);
			}
		}
		if(checkInventorySkuList!=null && checkInventorySkuList.size()>0) {
			for(CheckInventorySku entity:checkInventorySkuList) {
				//外键设置
				entity.setInventoryOrder(checkInventory.getInventoryOrder());
				checkInventorySkuMapper.insert(entity);
			}
		}
		if(checkInventoryResultList!=null && checkInventoryResultList.size()>0) {
			for(CheckInventoryResult entity:checkInventoryResultList) {
				//外键设置
				entity.setInventoryOrder(checkInventory.getInventoryOrder());
				checkInventoryResultMapper.insert(entity);
			}
		}
		if(checkInventoryDifferenceList!=null && checkInventoryDifferenceList.size()>0) {
			for(CheckInventoryDifference entity:checkInventoryDifferenceList) {
				//外键设置
				entity.setInventoryOrder(checkInventory.getInventoryOrder());
				checkInventoryDifferenceMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		checkInventoryShelfMapper.deleteByMainId(id);
		checkInventorySkuMapper.deleteByMainId(id);
		checkInventoryResultMapper.deleteByMainId(id);
		checkInventoryDifferenceMapper.deleteByMainId(id);
		checkInventoryMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			checkInventoryShelfMapper.deleteByMainId(id.toString());
			checkInventorySkuMapper.deleteByMainId(id.toString());
			checkInventoryResultMapper.deleteByMainId(id.toString());
			checkInventoryDifferenceMapper.deleteByMainId(id.toString());
			checkInventoryMapper.deleteById(id);
		}
	}
	
}
